package com.lovo.common.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableRabbit
public class DirectConfiguation {
    /**
     * 创建直连交换机
     * @return
     */
    @Bean
    public Exchange directExchange(){
        return new DirectExchange("shop.direct.exchange");
    }

    /**
     * 创建队列
     */
    @Bean
    public Queue directQueue(){
        return new Queue("shop.direct.queue");
    }

    /**
     * 注册队列
     */
    @Bean
    public Queue registerQueue(){
        Map map = new HashMap();
        map.put("x-max-length", 10);
        map.put("x-dead-letter-exchange","dead.exchange");
        map.put("x-dead-letter-routing-key","rk.dead");
        return new Queue("register.queue",true,false,false,map);
    }
    /**
     * 秒杀队列
     */
    @Bean Queue seckillQueue(){
        Map map = new HashMap();
        map.put("x-max-length", 10);
        return new Queue("seckill.queue",true,false,false,map);
    }
    /**
     * 将交换机和队列绑定
     */
    @Bean
    public Binding directQueueBinding(){
        return BindingBuilder.bind(directQueue()).to(directExchange()).with("rk.direct").noargs();
    }

    @Bean
    public Binding registerQueueBinding(){
        return BindingBuilder.bind(registerQueue()).to(directExchange()).with("rk.register").noargs();
    }
    @Bean
    public Binding seckillQueueBinding(){
        return BindingBuilder.bind(seckillQueue()).to(directExchange()).with("rk.seckill").noargs();
    }
}
